<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap ">
        <!--- <img src="./assets/logo.png" alt="">--->
        <MyHeader @AddTodo="AddTodo" />
        <!---   <MyList :todos="todos" :CheckTodo="CheckTodo" :deleteTodo="deleteTodo"/>--->
        <MyList :todos="todos" />
        <MyFooter :todos="todos" :checkAllTodo="checkAllTodo"  :clearAllTodo="clearAllTodo"/>
      </div>
    </div>
  
<!--     <School ref="str" ></School>-->
<!--     <button @click="showRef"></button>-->
<!--    <Student></Student>-->
<!--     <hr>-->
<!--     <student-detail name="weiwei" :age="18" sex="female"></student-detail>-->
  </div>
</template>

<script>
import pubsub from 'pubsub-js'
import 'animate.css'
// import School from './components/School.vue'
// import Student from './components/Student.vue'
// import StudentDetail from './components/StudentDetail.vue'
import MyHeader from './components/MyHeader.vue'
import MyList from './components/MyList.vue'
import MyFooter from './components/MyFooter.vue'

export default {
    name:'App',
    template:'<App></App>',
    components:{
        // School,
        // Student,
        // 
        MyHeader,
        MyList,
        MyFooter
    },
    methods: {
    //   showRef(){
    //     console.log(this.$refs)
    //   }
    // },
      // CheckTodo(id){
      //   this.todos.forEach (item =>{
      //     if(item.id === id) item.done = !item.done
      //   })
      // }, 
      AddTodo(todoObj){
        // console.log(x)
        this.todos.unshift(todoObj)
      },
      // deleteTodo(id){
      //   this.todos = this.todos.filter(todo => todo.id !== id)
      // },
      checkAllTodo(done){
        this.todos.forEach((todo =>{
          todo.done = done
        }))
      },
      clearAllTodo(){
        this.todos = this.todos.filter( todo => !todo.done)
      }
    },
   
     data() {
        return {
            todos:JSON.parse(localStorage.getItem('todos')) || []
      }
    },
    watch:{
      todos:{
        deep:true,
        handler(value){
          return localStorage.setItem('todos',JSON.stringify(value))
        }
      }
    },
    mounted() {
      // this.$bus.$on('CheckTodo',id=>{
      //    this.todos.forEach (item =>{
      //     if(item.id === id) item.done = !item.done
      //   })
      // }),
      this.pubid1 = pubsub.subscribe('CheckTodo',(msgName,id)=>{
         this.todos.forEach (item =>{
          if(item.id === id) item.done = !item.done
        })
      }),
      // this.$bus.$on('deleteTodo',id=>{
      //   this.todos = this.todos.filter(todo => todo.id !== id)
      // }),
      this.pubid2 = pubsub.subscribe('deleteTodo',(msgName,id)=>{
        this.todos = this.todos.filter(todo => todo.id !== id)
      })
      this.pubid3 = pubsub.subscribe('updateTodo',(msgName,id,data)=>{
        this.todos = this.todos.forEach(todo =>{
          if(todo.id === id) todo.title = data
        })
      })
    },
    beforeDestroy() {
      // this.$bus.$off('CheckTodo')
      // this.$bus.$off('deleteTodo')
      pubsub.unsubscribe(this.pubid1)
      pubsub.unsubscribe(this.pubid2)
      pubsub.unsubscribe(this.pubid3)


    },
}
</script>

<style>
  body{
    background-color: #fff;
  }
  .btn{
    display:inline-block;
    padding: 4px  12px;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    vertical-align: center;
    cursor: pointer;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.2),0 1px 2px rgba(0,0,0,0.05);
    border-radius: 4px;
    margin-bottom: 0;
  }
  .btn-danger{
    color: #fff;
    background-color: #da4f49;
    border: 1px solid #bd362f;
  }
  .btn-edit{
    color: #fff;
    background-color: #bfa;
    border: 1px solid #bf1;
  }
  .btn-danger :hover{
    color:#fff;
    background-color: #bd362f;
  }
  .btnLfocus{
    outline: none;
  }
  .todo-container{
    width: 600px;
    margin: 0 auto;
  }
  .todo-container .todo-wrap{
    padding: 10px;
    border:1px solid #ddd;
    border-raduis:5px
  }
</style>